﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Text;
using System.Web.Mvc.Html;
using System.Web.Mvc;

namespace Longtop.Web.Helpers {
    public static partial class MikePagerExtensions {



        #region MikePager 分页控件

        public static string MikePager<T>(this HtmlHelper html, PagedList<T> data) {
            string actioinName = html.ViewContext.RouteData.GetRequiredString("action");
            return MikePager<T>(html, data, actioinName);
        }

        public static string MikePager<T>(this HtmlHelper html, PagedList<T> data, object values) {
            string actioinName = html.ViewContext.RouteData.GetRequiredString("action");
            return MikePager<T>(html, data, actioinName, values);
        }

        public static string MikePager<T>(this HtmlHelper html, PagedList<T> data, string action) {
            return MikePager<T>(html, data, action, null);
        }


        public static string MikePager<T>(this HtmlHelper html, PagedList<T> data, string action, object values) {
            string controllerName = html.ViewContext.RouteData.GetRequiredString("controller");
            return MikePager<T>(html, data, action, controllerName, values);
        }
        public static string MikePager<T>(this HtmlHelper html, PagedList<T> data, string action, string controller, object values) {
            return MikePager<T>(html, data, action, controller, new RouteValueDictionary(values));
        }


        public static string MikePager<T>(this HtmlHelper html, PagedList<T> data, RouteValueDictionary values) {
            string actioinName = html.ViewContext.RouteData.GetRequiredString("action");
            return MikePager<T>(html, data, actioinName, values);
        }


        public static string MikePager<T>(this HtmlHelper html, PagedList<T> data, string action, RouteValueDictionary values) {
            string controllerName = html.ViewContext.RouteData.GetRequiredString("controller");
            //if (html.ViewContext.RouteData.Values["lang"] == null)
            return MikePager<T>(html, data, action, controllerName, values);
            //return CulturePager<T>(html, data, action, controllerName, values);
        }

        public static string MikePager<T>(this HtmlHelper html, PagedList<T> data, string action, string controller, RouteValueDictionary valuedic) {
            int start = (data.PageIndex - 5) >= 1 ? (data.PageIndex - 5) : 1;
            int end = (data.TotalPage - start) > 9 ? start + 9 : data.TotalPage;


            RouteValueDictionary vs = valuedic == null ? new RouteValueDictionary() : valuedic;

            var builder = new StringBuilder();
            builder.AppendFormat("<div class=\"mike_mvc_pager\">每页" + data.PageSize + "条/共" + data.TotalCount + "条 第" + data.PageIndex + "页/共" + data.TotalPage + "页 ");

            vs["pageSize"] = data.PageSize;
            if (data.IsPreviousPage) {
                vs["pageIndex"] = 1;
                builder.Append(html.ActionLink("首页", action, controller, vs, null));
                vs["pageIndex"] = data.PageIndex - 1;
                builder.Append(html.ActionLink("上一页", action, controller, vs, null));
            }

            for (int i = start; i <= end; i++) //前后各显示5个数字页码
            {
                vs["pageIndex"] = i;
                if (i == data.PageIndex) {
                    //builder.Append(html.ActionLink("<font class='thispagethis'>" + i.ToString() + "</font>", action, controller, vs, null) );
                    builder.Append("<font class='thispagethis'>" + i.ToString() + "</font>");

                }
                else {
                    builder.Append(html.ActionLink(i.ToString(), action, controller, vs, null));
                }
            }

            if (data.IsNextPage) {
                vs["pageIndex"] = data.PageIndex + 1;
                builder.Append(html.ActionLink("下一页", action, controller, vs, null));
                vs["pageIndex"] = data.TotalPage;
                builder.Append(html.ActionLink("末页", action, controller, vs, null));
            }
            builder.Append("</div>");
            return builder.ToString();
        }


        //public static string CulturePager<T>(this HtmlHelper html, PagedList<T> data, string action, string controller, RouteValueDictionary valuedic) {
        //    int start = (data.PageIndex - 5) >= 1 ? (data.PageIndex - 5) : 1;
        //    int end = (data.TotalPage - start) > 9 ? start + 9 : data.TotalPage;


        //    RouteValueDictionary vs = valuedic == null ? new RouteValueDictionary() : valuedic;

        //    var builder = new StringBuilder();
        //    builder.AppendFormat("<div class=\"mike_mvc_pager\">{0}" + data.PageSize + "{1}" + data.TotalCount + "{2}" + data.PageIndex + "{3}" + data.TotalPage + "{4} "
        //        , html.Local("每页"), html.Local("条/共"), html.Local("条 第"), html.Local("页/共"), html.Local("页")
        //        );

        //    vs["pageSize"] = data.PageSize;
        //    if (data.IsPreviousPage) {
        //        vs["pageIndex"] = 1;
        //        builder.Append(html.ActionLink(html.Local("分页首页"), action, controller, vs, null));
        //        vs["pageIndex"] = data.PageIndex - 1;
        //        builder.Append(html.ActionLink(html.Local("上一页"), action, controller, vs, null));
        //    }

        //    for (int i = start; i <= end; i++) //前后各显示5个数字页码
        //    {
        //        vs["pageIndex"] = i;
        //        if (i == data.PageIndex) {
        //            //builder.Append(html.ActionLink("<font class='thispagethis'>" + i.ToString() + "</font>", action, controller, vs, null) );
        //            builder.Append("<font class='thispagethis'>" + i.ToString() + "</font>");

        //        }
        //        else {
        //            builder.Append(html.ActionLink(i.ToString(), action, controller, vs, null));
        //        }
        //    }

        //    if (data.IsNextPage) {
        //        vs["pageIndex"] = data.PageIndex + 1;
        //        builder.Append(html.ActionLink(html.Local("下一页"), action, controller, vs, null));
        //        vs["pageIndex"] = data.TotalPage;
        //        builder.Append(html.ActionLink(html.Local("末页"), action, controller, vs, null));
        //    }
        //    builder.Append("</div>");
        //    return builder.ToString();
        //}
        #endregion

    }
}
